package com.ff.javamonkey.test.ui;

import java.io.File;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ff.javamonkey.R;

public class BluetoothConnectActivity extends ListActivity {
	// Debugging
	private static final String TAG = "BluetoothTestActivity";
	
	private enum DISPLAYMODE { ABSOLUTE, RELATIVE };
	private final DISPLAYMODE mDisplayMode = DISPLAYMODE.ABSOLUTE;
	private ArrayList<String> mDirectoryEntries = new ArrayList<String>();
	private File mCurrentDirectory = new File("/");
	
	// Constant
	public static final int RESULT_ID = 567;
	public static final int REQUEST_FILEPATH = 789;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		browseToRoot();
	}
	
	private void browseToRoot() {
		browseTo(new File("/"));
	}
	
	private void browseTo(final File dir) {
		if (dir.isDirectory()) {
			mCurrentDirectory = dir;
			fill(mCurrentDirectory.listFiles());
		}
		else {
			OnClickListener okButtonListener = new OnClickListener() {
				public void onClick(DialogInterface arg0, int arg1) {
					openFile(dir);
				}
			};
			
			OnClickListener cancelButtonListener = new OnClickListener() {
				public void onClick(DialogInterface arg0, int arg1) {
					// Do nothing;
				}
			};
			
			new AlertDialog.Builder(this)
				.setTitle("Question")
				.setMessage("Do you want to open this file?\n" + dir.getName())
				.setPositiveButton("OK", okButtonListener)
				.setNegativeButton("Cancel", cancelButtonListener)
				.show();
		}
	}
	
	private void fill(File[] files) {
		this.mDirectoryEntries.clear();
		this.mDirectoryEntries.add(getString(R.string.current_level));
		if (this.mCurrentDirectory.getParent()!=null)
			this.mDirectoryEntries.add(getString(R.string.up_one_level));
		switch(this.mDisplayMode) {
			case ABSOLUTE:
				for (File file : files) {
					this.mDirectoryEntries.add(file.getPath());	
				}
			break;
			
			case RELATIVE:
				int currentPathStringLength = this.mCurrentDirectory.getAbsolutePath().length();
				for (File file : files) {
					this.mDirectoryEntries.add(file.getAbsolutePath().substring(currentPathStringLength));
				}
			break;
		}
		
		ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.file_row, this.mDirectoryEntries);
		this.setListAdapter(directoryList);
	}
	
	private void openFile(File f) {
		Intent i = new Intent();
		i.putExtra("file", f);
		setResult(RESULT_ID, i);
		finish();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		int selectedIDRow = position;
		String selectedFileString = this.mDirectoryEntries.get(selectedIDRow);
		if (selectedFileString.equals(getString(R.string.current_level))) {
			// Refresh
			this.browseTo(this.mCurrentDirectory);
		}
		else if (selectedFileString.equals(getString(R.string.up_one_level))) {
			this.upOneLevel();
		}
		else {
			File selectedFile = null;
			switch (this.mDisplayMode) {
				case RELATIVE:
					selectedFile = new File(this.mCurrentDirectory.getAbsolutePath()+
							this.mDirectoryEntries.get(selectedIDRow));
				break;
				case ABSOLUTE:
					selectedFile = new File(selectedFileString);
				break;
			}
			if (selectedFile != null) {
				this.browseTo(selectedFile);
			}
		}
	}
	
	/**
     * This function browses up one level according to the field: mCurrentDirectory.
     */
    private void upOneLevel()
    {
         if(this.mCurrentDirectory.getParent() != null)
              this.browseTo(this.mCurrentDirectory.getParentFile());
    }
}
